local pingliao = fk.CreateSkill {

  name = "joy__pingliao",

  tags = { Skill.Compulsory, },

}



pingliao:addEffect(fk.CardUsing, {
  name = "joy__pingliao",
  anim_type = "control",
  events = {fk.CardUsing},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return player == target and player:hasSkill(pingliao.name) and data.card.trueName == "slash"
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getAlivePlayers(), function (p)
      return player:inMyAttackRange(p)
    end)
    room:doIndicate(player.id, table.map(targets, Util.IdMapper))
    local tos = data:getAllTargets()
    local drawcard = false
    local targets2 = {}
    for _, p in ipairs(targets) do
      local card = room:askForResponse(p, pingliao.name, ".|.|heart,diamond|.|.|basic", "#joy__pingliao-ask:" .. player.id, true)
      if card then
        room:responseCard{
          from = p.id,
          card = card
        }
        if not table.contains(tos, p.id) then
          drawcard = true
        end
      elseif table.contains(tos, p.id) then
        table.insert(targets2, p)
      end
    end
    for _, p in ipairs(targets2) do
      room:setPlayerMark(p, "@@joy__pingliao-turn", 1)
    end
    if player.dead then return false end
    if drawcard then
      player:drawCards(2, pingliao.name)
      room:addPlayerMark(player, MarkEnum.SlashResidue .. "-phase")
    end
  end,
})
pingliao:addEffect(fk.PreCardUse, {
  refresh_events = {fk.PreCardUse},
  can_refresh = function (self, event, target, player, data)
    return player == target and player:hasSkill(pingliao.name) and data.card.trueName == "slash"
  end,
  on_refresh = function (self, event, target, player, data)
    data.noIndicate = true
  end,
})

pingliao:addEffect("prohibit", {
  name = "#joy__pingliao_prohibit",
  prohibit_use = function(self, player, card)
    if player:getMark("@@joy__pingliao-turn") > 0 then
      local subcards = card:isVirtual() and card.subcards or {card.id}
      return #subcards > 0 and table.every(subcards, function(id)
        return table.contains(player:getCardIds(Player.Hand), id)
      end)
    end
  end,
  prohibit_response = function(self, player, card)
    if player:getMark("@@joy__pingliao-turn") > 0 then
      local subcards = card:isVirtual() and card.subcards or {card.id}
      return #subcards > 0 and table.every(subcards, function(id)
        return table.contains(player:getCardIds(Player.Hand), id)
      end)
    end
  end,
})

return pingliao